#include "histogram.h"

#include <qvalueaxis.h>

Histogram::Histogram() {}
// 数据分组函数
QVector<int> Histogram::computeHistogram(const QVector<double>& data, int numBins, double minVal, double maxVal) {
    QVector<int> histogram(numBins, 0);
    double binWidth = (maxVal - minVal) / numBins;

    for (double value : data) {
        int binIndex = static_cast<int>((value - minVal) / binWidth);
        if (binIndex >= 0 && binIndex < numBins) {
            histogram[binIndex]++;
        }
    }
    return histogram;
}

QVector<double> Histogram::computeBinEdges(double minVal, double maxVal, int numBins) {
    QVector<double> binEdges(numBins + 1);
    double binWidth = (maxVal - minVal) / numBins;

    for (int i = 0; i <= numBins; ++i) {
        binEdges[i] = minVal + i * binWidth;
    }
    return binEdges;
}
// 绘图函数
void Histogram::plotHistogram(QCustomPlot* customPlot, const QVector<int>& histogram, const QVector<double>& binEdges,QString titleName,QString xAxisName,QString yAxisName) {
    QCPBars* bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    QVector<double> xData, yData;
    bars->setWidth(binEdges[1]-binEdges[0]); // 宽度为 x 轴单位长度的 40%
    // 设置图表标题和轴标签
    customPlot->plotLayout()->insertRow(0);
    customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, titleName));
    customPlot->xAxis->setLabel(xAxisName);
    customPlot->yAxis->setLabel(yAxisName);
    for (size_t i = 0; i < histogram.size();  ++i) {
        xData.append((binEdges[i]  + binEdges[i + 1]) / 2); // 区间中点作为x轴
        yData.append(histogram[i]);  // 区间计数作为y轴
    }

    bars->setData(xData, yData);
    customPlot->rescaleAxes();
    customPlot->replot();
}


void Histogram::plotHistogram(QChartView* chartView, const std::vector<int>& histogram, const std::vector<double>& binEdges) {
    QBarSeries* series = new QBarSeries();

    for (size_t i = 0; i < histogram.size();  ++i) {
        QBarSet* set = new QBarSet(QString("Bin %1").arg(i));
        *set << histogram[i];
        series->append(set);
    }

    QChart* chart = new QChart();
    chart->addSeries(series);
    chart->setTitle("Histogram");
    chart->setTitleFont(QFont("微软雅黑", 14, QFont::Bold, false));
    chart->setTitleBrush(QColor(255,255,255));
    chart->setAnimationOptions(QChart::SeriesAnimations);
    series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd); // 设置数据系列标签的位置于数据柱内测上方
    series->setLabelsVisible(true); // 设置显示数据系列标签


    chart->createDefaultAxes();
    chart->legend()->setVisible(false);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->setBackgroundVisible(false);
    chart->axisX()->setGridLineVisible(false);
    chart->axisX()->setLabelsColor(QColor(255,255,255));
    chart->axisX()->setLabelsFont(QFont("微软雅黑", 12, QFont::Normal, false));
    chart->axisX()->setTitleFont(QFont("微软雅黑", 12, QFont::Normal, false));
    QValueAxis *axisY = new QValueAxis();
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);
    chart->axisY()->setGridLineVisible(true);
    chart->axisY()->setGridLineColor(QColor(156, 82, 225,120));
    chart->axisY()->setTitleText("");
    chart->axisY()->setLabelsColor(QColor(255,255,255));
    chart->axisY()->setLabelsFont(QFont("微软雅黑", 12, QFont::Normal, false));
    chart->axisY()->setVisible(true);
    chart->axisY()->setTitleBrush(QColor(255,255,255));//设置x轴字体颜色
    chart->axisY()->setTitleFont(QFont("微软雅黑", 12, QFont::Normal, false));
    chart->axisY()->setRange(0,100);
    //    chart->setTitle("直方图");

    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->legend()->setLabelColor(QColor(255,255,255));
    chart->legend()->setFont(QFont("微软雅黑",12,true));

    chartView->setChart(chart);
}

void Histogram::plotHistogram(QChartView* chartView, const std::vector<int>& histogram) {
    // 1. 创建数据系列
    QBarSeries* series = new QBarSeries();
    series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);
    series->setLabelsVisible(true);

    // 添加柱状图数据
    for (size_t i = 0; i < histogram.size();  ++i) {
        QBarSet* set = new QBarSet(QString("Bin %1").arg(i));
        *set << histogram[i];
        series->append(set);
    }

    // 2. 创建并配置图表
    QChart* chart = new QChart();
    chart->addSeries(series);
    chart->setTitle("Histogram");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->setBackgroundVisible(false);

    // 3. 配置图表样式
    setupChartStyle(chart);

    // 4. 配置坐标轴
    setupAxes(chart, series);

    // 5. 配置图例
    setupLegend(chart);

    // 6. 将图表设置到视图
    chartView->setChart(chart);
}

// 辅助函数：配置图表样式
void Histogram::setupChartStyle(QChart* chart) {
    QFont titleFont("微软雅黑", 14, QFont::Bold);
    chart->setTitleFont(titleFont);
    chart->setTitleBrush(Qt::white);
}

// 辅助函数：配置坐标轴（兼容最新Qt）
void Histogram::setupAxes(QChart* chart, QBarSeries* series) {
    // 创建默认坐标轴
    chart->createDefaultAxes();
    // X轴配置（使用替代API）
    QList<QAbstractAxis*> xAxes = chart->axes(Qt::Horizontal);
    if (!xAxes.isEmpty())  {
        QAbstractAxis* axisX = xAxes.first();
        axisX->setGridLineVisible(false);
        axisX->setLabelsColor(Qt::white);
        axisX->setLabelsFont(QFont("微软雅黑", 12));
    }

    // Y轴配置（使用替代API）
    QList<QAbstractAxis*> yAxes = chart->axes(Qt::Vertical);
    QValueAxis* axisY = !yAxes.isEmpty()  ? qobject_cast<QValueAxis*>(yAxes.first())  : nullptr;
    if (!axisY) {
        axisY = new QValueAxis();
        chart->addAxis(axisY, Qt::AlignLeft);
        series->attachAxis(axisY);
    }
    // Y轴样式设置
    axisY->setGridLineVisible(true);
    axisY->setGridLineColor(QColor(156, 82, 225, 120));
    axisY->setLabelsColor(Qt::white);
    axisY->setLabelsFont(QFont("微软雅黑", 12));
    axisY->setRange(0, 100);
    axisY->setTitleText("");
    axisY->setTitleBrush(Qt::white);
    axisY->setTitleFont(QFont("微软雅黑", 12));
}

// 辅助函数：配置图例
void Histogram::setupLegend(QChart* chart) {
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->legend()->setLabelColor(Qt::white);
    chart->legend()->setFont(QFont("微软雅黑", 12, QFont::Bold));
}
